//
//  PublishDetailView.m
//  Find
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 Reiko. All rights reserved.
//

#import "PublishDetailView.h"

@implementation PublishDetailView


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    
        
    }
    return self;
}

#pragma mark -- 布局视图
- (void)addSubViews
{
    self.titleTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, (kScreenHeight-329)*0.03, kScreenWidth-20, (kScreenHeight-329)*0.25)];

    self.titleTextView.font = [UIFont systemFontOfSize:18];
    self.titleTextView.delegate = self;
    self.titleTextView.layer.borderColor = [UIColor colorWithRed:87/255.0 green:191/255.0 blue:255/255.0 alpha:1].CGColor;
    self.titleTextView.layer.borderWidth =1.0;//该属性显示外边
    self.titleTextView.layer.cornerRadius = 6.0;//通过该值来设置textView边角的弧度
    self.titleTextView.layer.masksToBounds = YES;
    self.titleTextView.font = [UIFont systemFontOfSize:19];
    
    [self addSubview:self.titleTextView];
    
    self.titlePlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, kScreenWidth - 20, 30)];
    self.titlePlaceholder.text = @"请输入标题(20字之内)";
    self.titlePlaceholder.font = [UIFont systemFontOfSize:16];
    self.titlePlaceholder.textColor = [UIColor lightGrayColor];
    [self.titleTextView addSubview:self.titlePlaceholder];
    
    
    self.contextTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.titleTextView.frame)+(kScreenHeight-329)*0.03, kScreenWidth - 20, (kScreenHeight-329)*0.39)];
    self.contextTextView.font = [UIFont systemFontOfSize:17];
    self.contextTextView.delegate = self;
    self.contextTextView.layer.borderColor = [UIColor colorWithRed:87/255.0 green:191/255.0 blue:255/255.0 alpha:1].CGColor;
    self.contextTextView.layer.borderWidth =1.0;//该属性显示外边
    self.contextTextView.layer.cornerRadius = 6.0;//通过该值来设置textView边角的弧度
    self.contextTextView.layer.masksToBounds = YES;
    self.contextTextView.textColor = [UIColor blackColor];
    self.contextTextView.textAlignment = NSTextAlignmentLeft;
    self.contextTextView.autoresizingMask
    = UIViewAutoresizingFlexibleHeight;
    [self addSubview:self.contextTextView];
    
    self.contextPlaceholder = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, kScreenWidth - 20, 30)];
    self.contextPlaceholder.text = @"请输入详细的信息";
    self.contextPlaceholder.font = [UIFont systemFontOfSize:15];
    self.contextPlaceholder.textColor = [UIColor lightGrayColor];
    [self.contextTextView addSubview:self.contextPlaceholder];

    self.photoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.contextTextView.frame)+5, kScreenWidth, 166-(kScreenHeight-329)*0.26)];
    self.photoScrollView.backgroundColor = [UIColor greenColor];
    self.photoScrollView.showsHorizontalScrollIndicator = NO;
    self.photoScrollView.showsVerticalScrollIndicator = NO;
    self.photoScrollView.contentSize = CGSizeMake(2*kScreenWidth, 10);
   // [self addSubview:self.photoScrollView];
    
    self.addressImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, kScreenHeight-113-(kScreenHeight-329)*0.075, (kScreenHeight-329)*0.075, (kScreenHeight-329)*0.075)];
   // self.addressImageView.backgroundColor = [UIColor orangeColor];
    self.addressImageView.image = [UIImage imageNamed:@"iconfont-address-@3x"];
    self.addressImageView.userInteractionEnabled = YES;
    [self addSubview:self.addressImageView];
    
    self.addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.addressImageView.frame), CGRectGetMinY(self.addressImageView.frame), kScreenWidth-45, CGRectGetHeight(self.addressImageView.frame))];
    self.addressTextField.font = [UIFont systemFontOfSize:14];
    self.addressTextField.userInteractionEnabled = NO;
    self.addressTextField.placeholder = @"地址";
  //  self.addressLabel.backgroundColor = [UIColor grayColor];
    [self addSubview:self.addressTextField];
    UIView *lineAddress = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.addressTextField.frame), CGRectGetMaxY(self.addressTextField.frame), CGRectGetWidth(self.addressTextField.frame), 1)];
    lineAddress.backgroundColor = kFindListCellLabelTextColor;
    [self addSubview:self.addressTextField];
    

    self.toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, kScreenHeight - 113, kScreenWidth, 49)];
    self.toolBar.barTintColor = [UIColor colorWithRed:0.341 green:0.749 blue:0.996 alpha:1.000];
        self.toolBar.alpha = 1;
    [self addSubview:self.toolBar];
    
  // @"photo.png",
    NSArray *imageArray = @[@"map.png",@"image.png"];
    for (int i = 0; i < imageArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 100 + i;
        button.frame = CGRectMake(60 + (i * 200), 10, 40, 30);
        [button setBackgroundImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal];
        [self.toolBar addSubview:button];
    }
    self.mapButton = (UIButton *)[self viewWithTag:100];
   // self.cameraButton = (UIButton *)[self viewWithTag:101];
    self.photoButton = (UIButton *)[self viewWithTag:101];
    
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(actionSwipe:)];
     // 设置左右扫
    swipe.direction = UISwipeGestureRecognizerDirectionDown;
     [self addGestureRecognizer:swipe];
}

- (void)actionSwipe:(UISwipeGestureRecognizer *)swipe
{
    [self.titleTextView resignFirstResponder];
    [self.contextTextView resignFirstResponder];
}
#pragma mark -- textView代理方法

- (void)textViewDidChange:(UITextView *)textView
{
    
    if ([textView isEqual:self.titleTextView]){
        self.titlePlaceholder.text = @"";
        self.titlePlaceholder.hidden = YES;
    } else {
        self.contextPlaceholder.text = @"";
        self.contextPlaceholder.hidden = YES;
    }
    
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = [notification userInfo];
    NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [value CGRectValue];
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [self autoMoveKeyBoard:keyboardRect.size.height AnimationTime:animationDuration];
    
    
}
- (void)keyboardDidHide:(NSNotification *)notification
{
     NSDictionary *userInfo = [notification userInfo];
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [self autoMoveKeyBoard:0 AnimationTime:animationDuration];

}
- (void)autoMoveKeyBoard:(float)height AnimationTime:(NSTimeInterval)time
{
    [UIView animateWithDuration:time animations:^{
        self.toolBar.frame = CGRectMake(0, kScreenHeight - height - 113, kScreenWidth, 49);
        self.addressImageView.frame = CGRectMake(10, kScreenHeight-height-113-(kScreenHeight-329)*0.075, (kScreenHeight-329)*0.075, (kScreenHeight-329)*0.075);
        self.addressTextField.frame = CGRectMake(CGRectGetMaxX(self.addressImageView.frame), CGRectGetMinY(self.addressImageView.frame), kScreenWidth-45, CGRectGetHeight(self.addressImageView.frame));
//        self.contextTextView.frame = CGRectMake(10, 5, kScreenWidth - 20, kScreenHeight - height - 130);
        
    }];
}

@end
